package com.example.untils;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.entity.hz.HttpResult;
import com.example.entity.hz.ShenHe;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: LiangPanDeng
 * @Date: 2024/01/09/17:14
 * @Description:    http请求工具类
 */
public class HttpClientUtil {

    private static String YF_SERVER_PORT = "http://192.168.1.47:9000/api/yifei/audit";

    /**
     * http post请求
     * @param url
     * @param aramsJson
     * @return  请求结果
     * @throws IOException
     */
    public static String postRequest(String url, String aramsJson) throws IOException {
        // 创建HttpPost对象
        System.out.println("创建HttpPost对象");
        HttpPost httpPost = new HttpPost(url);
        System.out.println("httpPost: " + httpPost);
        httpPost.setHeader("Content-Type", "application/json;charset=utf8");

        // 将参数转换为json字符串并设置到httpPost实体中
        //String jsonParams = objectMapper.writeValueAsString(params);
        httpPost.setEntity(new StringEntity(aramsJson, "UTF-8"));

        System.out.println("发送请求并获取响应");

        System.out.println("http:" + httpPost);
        System.out.println(httpPost.getEntity());

        // 设置连接超时时间和套接字超时时间
        RequestConfig requestConfig = RequestConfig.custom()
                .setConnectTimeout(5000)
                .setSocketTimeout(5000)
                .build();
        httpPost.setConfig(requestConfig);


        // 发送请求并获取响应
        //CloseableHttpResponse response = httpClient.execute(httpPost);
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        CloseableHttpResponse response;
        response =  httpClient.execute(httpPost);
        //CloseableHttpResponse response = httpClient.execute(httpPost)
        System.out.println("response结果 :" + response);

        // 获取响应体
        String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
        //System.out.println("res");
        System.out.println("responseBody :" + responseBody);
        return responseBody;

    }

    public static HttpResult postHZ(ShenHe shenHe) {
        System.out.println(YF_SERVER_PORT);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Content-Type","application/json; charset=utf-8");
        HttpEntity<String> httpEntity = new HttpEntity<>(JSONUtil.toJsonStr(shenHe), httpHeaders);
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.postForObject(YF_SERVER_PORT, httpEntity, HttpResult.class);
    }

}
